from selenium import webdriver
from selenium.webdriver import ChromeOptions
import time
import re
import xlsxwriter
import pymysql.cursors

connection = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='1940952395', db='boss',
                             charset='utf8', cursorclass=pymysql.cursors.DictCursor)

cur = connection.cursor()
url = []
second_url = 'https://www.zhipin.com'


def seach():
    driver.get('https://login.zhipin.com/?ka=header-login')
    time.sleep(15)
    # driver.find_element_by_class_name('placeholder').click()
    # driver.find_element_by_id('tab-4').click()
    # driver.find_element_by_xpath('//*[@id="pane-4"]/ul/li[2]/ul/li[7]/span').click()
    driver.find_element_by_xpath('//*[@id="container"]/div[2]/div/div[1]/div/div[2]/div/div[1]/input').send_keys('python')
 #   driver.find_element_by_class_name("ipt-search").send_keys('python')
    driver.find_element_by_class_name("ui-icon-search").click()


def drop_down():
    for x in range(1, 11, 2):
        time.sleep(0.5)
        j = x / 10
        js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
        driver.execute_script(js)


def geturl():
    seach()
    num = 1
    while num != 8:
        driver.get(
            'https://www.zhipin.com/c101230500/b_鲤城区/?query={}&page={}&ka=page-{}'.format(keyword, num, num))  # 切换城市只需要更改城市代码即可
        driver.implicitly_wait(10)
        num += 1
        lis = driver.find_elements_by_xpath(
            '//div[@class="job-primary"]/div[@class="info-primary"]')
        drop_down()
        time.sleep(8)
        for li in lis:
            src = li.find_element_by_xpath('.//div[@class="primary-wrapper"]/div[@class="primary-box"]').get_attribute(
                'href')
            urls = second_url + src
            print(urls)
            url.append(urls)
    return url

def res_url(url):
    for rl in url:
        driver.get(rl)
        ril = driver.find_elements_by_xpath('//*[@id="main"]')
        for ri in ril:
            try:
                job_name = ri.find_element_by_xpath(
                    './/div/div/div/div[2]/div[2]/h1').text
                salary = ri.find_element_by_xpath(
                    './/div/div/div/div[2]/div[2]/span').text
                adders = ri.find_element_by_xpath(
                    './/div/div/div/div[2]/p').text
                fuli = ri.find_elements_by_xpath(
                    './/div/div/div/div[2]/div[3]//div[@class="job-tags"]/span')
                for i in range(len(fuli)):
                    fuli[i] = fuli[i].text
                hr_name = ri.find_element_by_xpath(
                    './/div[3]/div/div[2]/div/h2').text
                hr_job = ri.find_element_by_xpath(
                    './/div[3]/div/div[2]/div/p').text
                desc = ri.find_element_by_xpath(
                    './/div[3]/div/div[2]/div[2]/div/div').text
                company = ri.find_element_by_xpath(
                    './/div[3]/div/div[2]/div[2]/div[2]/div').text
                workadders = ri.find_element_by_xpath(
                    './/div[3]/div/div[2]/div[2]/div[@class="job-sec"]/div[@class="job-location"]/div[@class="location-address"]').text
      #          companyname1 = ri.find_element_by_xpath('.//div[3]/div/div[2]/div[2]/div[@class="job-sec"]/div[1]').text
      #           faren = ri.find_element_by_xpath('.//div[3]/div/div[2]/div[2]/div[@class="job-sec"]/div[2]/li[1]').text
      #           zijin = ri.find_element_by_xpath('.//div[3]/div/div[2]/div[2]/div[@class="job-sec"]/div[2]/li[2]').text
      #           comtime = ri.find_element_by_xpath('.//div[3]/div/div[2]/div[2]/div[@class="job-sec"]/div[2]/li[3]').text
      #           qiyecay = ri.find_element_by_xpath('.//div[3]/div/div[2]/div[2]/div[@class="job-sec"]/div[2]/li[4]').text
      #           stamert = ri.find_element_by_xpath('.//div[3]/div/div[2]/div[2]/div[@class="job-sec"]/div[2]/li[5]').text
             #   comimg = ri.find_element_by_xpath('.//div[3]/div/div/div[2]/div/a[1]/img').get_attribute('src')
                companyname = ri.find_element_by_xpath(
                    './/div[3]/div/div/div/div[1]/a[2]').text
      #          rongzi = ri.find_element_by_xpath('.//div[3]/div/div/div/p[2]').text
                persons = ri.find_element_by_xpath('.//div[3]/div/div/div//i[@class="icon-scale"]/..').text
                comcagery = ri.find_element_by_xpath(
                    './/div[3]/div/div/div//i[@class="icon-industry"]/..').text
      #          companyurl = ri.find_element_by_xpath('.//div[3]/div/div/div/p[5]').text
                updatetime = ri.find_element_by_xpath(
                    './/div[3]/div/div/div/p[last()]').text
                boss = {
                    'job_name': job_name,
                    'salary': salary,
                    'request': adders,
                    'hrname': hr_name,
                    'hr_job': hr_job,
                    'fuli': fuli,
                    'desc': desc,
                    'adders': workadders,
                    'company': company,
           #         'company1': companyname1,
           #          'faren': faren,
           #          'zijin': zijin,
           #          'comtime': comtime,
           #          'qiyecay': qiyecay,
           #          'stamert': stamert,
                 #   'companyimg': comimg,
                    'companyname': companyname,
                    'comcagery': comcagery,
            #        'rongzi': rongzi,
                    'person': persons,
            #        'companurl': companyurl,
                    'update': updatetime
                }
                print(boss)
                sql = "insert into job1(jobname,request,salary,jobdesc,workadders,persons,companycay,updatetime,company1) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
                params = (boss["job_name"], boss["request"], boss["salary"], boss["desc"], boss["adders"], boss["person"], boss["comcagery"],boss["update"],boss["companyname"])
                cur.execute(sql, params)
                connection.commit()
                time.sleep(8)
            except Exception as e:
                print(e)


if __name__ == '__main__':
    keyword = input('请输入您要职位信息:')
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    driver = webdriver.Chrome(options=option)
    geturl()
    res_url(url)
    cur.close()
    connection.close()
